package com.cgwx.tcservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cgwx.servicebase.exceptionhandler.MyException;
import com.cgwx.tcservice.entity.tc.TcDirectFrameConfig;
import com.cgwx.tcservice.entity.vo.TcDirectFrameExcelVo;
import com.cgwx.tcservice.listener.TcDirectFrameExcelListener;
import com.cgwx.tcservice.mapper.TcDirectFrameConfigMapper;
import com.cgwx.tcservice.service.TcDirectFrameConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * <p>
 * 直接指令遥控指令、参数配置表 服务实现类
 * </p>
 *
 * @author GaoWei
 * @since 2023-09-14
 */
@Service
public class TcDirectFrameConfigServiceImpl extends ServiceImpl<TcDirectFrameConfigMapper, TcDirectFrameConfig> implements TcDirectFrameConfigService {

//    更新遥控指令表配置
    @Override
    public void updateTcDirectFrameConfig(MultipartFile file,TcDirectFrameConfigService tcDirectFrameConfigService) {
//        查询是否有配置
        Integer queryCount = baseMapper.selectCount(null);
        if (queryCount != 0){
            baseMapper.delete(null);
        }
//        读取Excel中的数据，插入到表中
        try {
            InputStream inputStream = file.getInputStream();
//            调用方法读取，读取一个sheet
            EasyExcel.read(inputStream, TcDirectFrameExcelVo.class, new TcDirectFrameExcelListener(tcDirectFrameConfigService)).sheet().doRead();
        } catch (Exception e) {
            throw new MyException(20004,"读取直接指令配置表格出错");
        }
    }
}
